package com.dhh.sky.widget;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.dhh.sky.R;
import com.dhh.sky.model.Operation;
import com.dhh.sky.model.SkyDriveObject;
import com.dhh.sky.model.SkyDrivePhoto;
import com.dhh.sky.util.CUtil;
import com.dhh.sky.util.Constant;
import com.dhh.sky.util.FileManager;
import com.dhh.sky.util.OperationManager;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TransitionTabOperationAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
	private DisplayImageOptions options;
	private ImageLoader imageLoader = ImageLoader.getInstance();
    private List<Operation> ops;
    private OperationManager om;
    private FileManager fm;
    private Context context;
    private int action;
    private String status;
	
	public TransitionTabOperationAdapter(Context context,int action,String status){
    	this.context = context;
    	this.action = action;
    	this.status = status;
    	om = new OperationManager(context);
    	fm = new FileManager(context);
    	ops = new ArrayList<Operation>();
    	mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        options = new DisplayImageOptions.Builder()
                                .showStubImage(R.drawable.image_picker_placeholder)
                                .cacheInMemory()
                                .cacheOnDisc()
                                .build();
    	loadOperation();
	}
	
	public void loadOperation(){
		if(Operation.OP_DOWNLOAD==action){
			if(Constant.TRANSFER_COMPLETED.equalsIgnoreCase(status)){
			}else if(Constant.TRANSFER_UNCOMPLETED.equalsIgnoreCase(status)){
				ops.addAll(om.getOperationFailed(Operation.OP_DOWNLOAD));
				ops.addAll(om.getOperationReady(Operation.OP_DOWNLOAD));
			}
			
			
		}else if(Operation.OP_UPLOAD==action){
			if(Constant.TRANSFER_COMPLETED.equalsIgnoreCase(status)){
				ops.addAll(om.getOperationSuccessed(Operation.OP_UPLOAD));
			}else if(Constant.TRANSFER_UNCOMPLETED.equalsIgnoreCase(status)){
				ops.addAll(om.getOperationFailed(Operation.OP_UPLOAD));
				ops.addAll(om.getOperationReady(Operation.OP_UPLOAD));
			}
		}
    }

	@Override
	public int getCount() {
		return ops.size();
	}

	@Override
	public Operation getItem(int position) {
		return ops.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final View view = convertView != null ? convertView : mInflater.inflate(R.layout.transfer_list_item,parent,false);
		final Operation op = this.getItem(position);
		ImageView icon = (ImageView) view.findViewById(R.id.oli_icon);
		TextView name = (TextView) view.findViewById(R.id.oli_name);
		TextView status = (TextView) view.findViewById(R.id.oli_status);
		status.setTextColor(Color.BLACK);
		ImageView more = (ImageView) view.findViewById(R.id.oli_more);
		
		if(Integer.valueOf(op.getStatus())==Operation.STATUS_SUCCESS){
			more.setVisibility(View.VISIBLE);
		}else{
			more.setVisibility(View.VISIBLE);
		}
		
		//icon.setImageResource(SkyDriveObject.getFileIcon(CUtil.getFileExtesion(op.getMeta())));
		setImageView(icon,action,op);
		
		name.setText(op.getMeta());
		if(Operation.OP_DOWNLOAD==action){
			if(Integer.valueOf(op.getStatus())==Operation.STATUS_READY){
				status.setText(context.getString(R.string.msg_waiting_download));
			}else if(Integer.valueOf(op.getStatus())==Operation.STATUS_ERROR){
				//TODO fail to download
				status.setText("Failed to download");
				status.setTextColor(Color.RED);
			}
		}else if(Operation.OP_UPLOAD==action){
			if(Integer.valueOf(op.getStatus())==Operation.STATUS_READY){
				status.setText(context.getString(R.string.msg_waiting_upload));
			}else if(Integer.valueOf(op.getStatus())==Operation.STATUS_ERROR){
				//TODO fail to upload
				status.setText("Failed to upload");
				status.setTextColor(Color.RED);
			}
		}else{
			status.setVisibility(View.GONE);
		}
		
		more.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				removeItem(op);				
				om.removeOperation(op.getPath(), Integer.valueOf(op.getAction()));
			}
		});
		
		return view;
	}
	
	public void setImageView(ImageView imageView,int action,Operation op){
		if(Operation.OP_UPLOAD==action){
			imageView.setImageResource(SkyDriveObject.getFileIcon(CUtil.getFileExtesion(op.getMeta())));
			if(Constant.pictureFileType.contains(CUtil.getFileExtesion(op.getMeta()))){
				File file = new File(op.getPath());
				if(file.exists()){
					imageView.setImageBitmap(CUtil.decodeBitmap(file, 100, 100));
				}
			}
		}else if(Operation.OP_DOWNLOAD==action){
			imageView.setImageResource(SkyDriveObject.getFileIcon(CUtil.getFileExtesion(op.getMeta())));
			if(Constant.pictureFileType.contains(CUtil.getFileExtesion(op.getMeta()))){
				SkyDriveObject obj = fm.getFile(op.getPath());
				if(obj!=null){
					imageLoader.displayImage(((SkyDrivePhoto)obj).getThumbnailSrc(), imageView, options);
				}
			}
		}else{
			imageView.setImageResource(SkyDriveObject.getFileIcon(CUtil.getFileExtesion(op.getMeta())));
		}
	}
	
	public void removeItem(Operation op){
		for(int i=0; i<ops.size(); i++){			
			if(op.getPath().equals(ops.get(i).getPath())){
				ops.remove(i);
				break;
			}
		}
		
		this.notifyDataSetChanged();
	}
}
